package com.ziyun.erp.web.api.order;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.e.order.*;
import com.ziyun.erp.modules.freightages.service.SdbEctoolsRegionsService;
import com.ziyun.erp.modules.logistics.service.LogisticsTypeService;
import com.ziyun.erp.modules.order.entity.*;
import com.ziyun.erp.modules.order.service.*;
import com.ziyun.erp.modules.order.vo.OrderInfoVo;
import com.ziyun.erp.modules.production.entity.ErpOrderProductionEntity;
import com.ziyun.erp.modules.production.service.ErpOrderProductionService;
import io.renren.common.utils.R;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;


/**
 * 订单基础信息表
 *
 * @author llc
 * @date 2018-09-20 10:40:47
 */
@RestController
@RequestMapping("port/order/orderinfo")
public class OrderInfoControllerApi {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private ErpOrderService erpOrderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private SdbEctoolsRegionsService sdbEctoolsRegionsService;
    @Autowired
    private LogisticsTypeService logisticsTypeService;
    @Autowired
    private ErpOrderProductionService erpOrderProductionService;

    /**
     * 列表
     */
    @RequestMapping("/mall/list")
    public R list(@RequestBody Map<String, Object> paramBodyMap){
        return orderInfoService.queryPageMallOrderList(paramBodyMap);
    }

    /**
     * 获取订单信息
     * @param
     * @return
     */
    @RequestMapping("/mall/queryOrderinfo")
    public R queryOrderinfo(@RequestBody Map<String, Object> paramBodyMap){
        String orderId = paramBodyMap.get("orderId").toString();
        return orderInfoService.querySellerOrderInfo(Long.valueOf(orderId));
    }

    /**
     * 获取物流信息
     * @param
     * @return
     */
    @RequestMapping("/mall/sendLogisticsInfo")
    public R sendLogisticsInfo(@RequestBody Map<String, Object> paramBodyMap){
        String orderId = paramBodyMap.get("orderId").toString();
        // 查询是否分配订单，若是分配订单，需判断生产状态是否生产完成
        ErpOrderProductionEntity erpOrderProduction = erpOrderProductionService.selectOne(new EntityWrapper<>(new ErpOrderProductionEntity())
                .eq("production_order", orderId)
        );
        if (erpOrderProduction != null){
            ErpOrderEntity erpOrder = erpOrderService.selectById(orderId);
            if (erpOrder == null){
                return R.error("订单尚未开始生产");
            }
            Integer productionStatus = erpOrder.getProductionStatus();
            if (productionStatus.intValue() != ProductionStatusEnum.PRODUCTION_FINISH.toInt()
                    && productionStatus.intValue() != ProductionStatusEnum.FINISH.toInt()){
                return R.error("订单尚未生产完成");
            }
        }
        // 订单信息
        OrderInfoVo orderInfoVo = orderInfoService.selectOrderInfoVoById(Long.valueOf(orderId));
        // 商品列表
        List<OrderItemEntity> orderItemEntityList = orderItemService.selectList(
                new EntityWrapper<OrderItemEntity>()
                        .eq("order_id",orderId));
        // 查询省市区级联数据
        List<String> area = sdbEctoolsRegionsService.queryRegionsSascade();
        // 物流类型
        List<Map<Integer,String>> logisticsTypeList = logisticsTypeService.queryTypeList();
        return  R.ok().put("oderInfoEntity", orderInfoVo)
                      .put("orderItemEntityList",orderItemEntityList)
                      .put("area",  JSONArray.fromObject(area).toString())
                      .put("logisticsTypeList", logisticsTypeList);
    }

}
